﻿using System;
using System.Web.Mvc;

namespace MvcTemplates
{
    public class HelpMessageAttribute : TagBuilderAttribute
    {
        public HelpMessageAttribute( string text )
        {
            Text = text;
        }

        public HelpMessageAttribute( Type resourceType, string resourceName )
        {
            Text = TemplateHelpers.GetResourceText( resourceType, resourceName );
        }

        public string Text { get; private set; }

        public override MvcHtmlString GetEndTag( HtmlHelper helper )
        {
            if ( TagBuilderType == null )
            {
                return helper.C( c => c.HelpMessage, TagRenderMode.EndTag );
            }

            return base.GetEndTag( helper );
        }

        public override MvcHtmlString GetStartTagAndInnerContent( HtmlHelper helper, string propertyName )
        {
            if ( TagBuilderType == null )
            {
                var mmd = ModelMetadata.FromStringExpression( propertyName, helper.ViewData );
               return helper.C( c => c.HelpMessage, TagRenderMode.StartTag, mmd.MergeAttributes( null, this.GetType() ),
                                     propertyName: propertyName );  
            }
            
            return base.GetStartTagAndInnerContent( helper, propertyName );            
        }

    }

    
}